package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.common.result.YiFeiException;
import com.example.config.WebSocketMessage;
import com.example.entity.hz.Messageconfiguration;
import com.example.entity.hz.Messagepz;
import com.example.entity.hz.Znx;
import com.example.enums.ZnxStatusEnum;
import com.example.mapper.CmsmvMapper;
import com.example.mapper.MessageconfigurationMapper;
import com.example.mapper.MessagepzMapper;
import com.example.mapper.ZnxMapper;
import com.example.service.MessagepzService;
import com.example.service.ZnxService;
import com.example.untils.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author 14501
* @description 针对表【messagePz】的数据库操作Service实现
* @createDate 2024-09-06 09:03:07
*/
@Service
@Slf4j
public class MessagepzServiceImpl extends ServiceImpl<MessagepzMapper, Messagepz>
    implements MessagepzService{

    @Resource
    private MessagepzMapper messagepzMapper;

    @Resource
    private MessageconfigurationMapper messageconfigurationMapper;

    @Resource
    private CmsmvMapper cmsmvMapper;

    @Resource
    private ZnxService znxService;
    @Resource
    private ZnxMapper znxMapper;

    @Override
    @Transactional
    public Result saveMessage(List<Messagepz> messagepzList) {
        try{
            if (!messagepzList.isEmpty()){
               messagepzList.forEach(arr -> {
                   this.messagepzMapper.insert(arr);
               });
               return Result.success("新增成功");
            }
            return Result.error("请输入正确的参数");
        }catch (Exception e){
            throw new YiFeiException(500, "消息配置新增发生异常"+e.getMessage());
        }
    }

    @Override
    public Result queryIPage(Integer current, Integer size) {
        try{
            Map<String,Object> map = new HashMap<>();
            PageHelper.startPage(current,size);
            List<Messagepz> messagepzList = this.messagepzMapper.selectList(null);
            PageInfo<Messagepz> messagepzPageInfo = new PageInfo<>(messagepzList);
            map.put("pages", messagepzPageInfo.getPages());
            map.put("total", messagepzPageInfo.getTotal());
            map.put("list", messagepzPageInfo.getList());
            return Result.success(map);
        }catch (Exception e){
            throw new YiFeiException(500,"消息配置分页发生异常"+e.getMessage());
        }
    }

    @Override
    public Result deleteMessage(List<Integer> uuidList) {
        try{
            boolean b = this.removeByIds(uuidList);
            if (b) {
                return Result.success();
            }
            return Result.error("删除失败");
        }catch (Exception e){
            throw new YiFeiException(500,"删除消息配置发生异常"+e.getMessage());
        }
    }

    @Override
    public Result<Messagepz> queryOne(Integer uuid) {
        try{
            Messagepz messagepz = this.messagepzMapper.selectById(uuid);
            return Result.success(messagepz);
        }catch (Exception e){
            throw new YiFeiException(500,"查询详情信息发生异常"+e.getMessage());
        }
    }

    @Override
    public Result updateMessage(Messagepz messagepz) {
        try{
            int update = this.messagepzMapper.updateById(messagepz);
            if (update>0){
                return Result.success();
            }
            return Result.error("修改失败");
        }catch (Exception e){
            throw new YiFeiException(500,"修改发生异常"+e.getMessage());
        }
    }

    @Override
    public Result PushMessage(String url,String resultString,String reqParam) {
        try{
//            Map<String, String> jsonMap = com.alibaba.fastjson2.JSON.parseObject(reqParam, new com.alibaba.fastjson2.TypeReference<HashMap<String, String>>() {
//            });
//            log.info("12345678"+jsonMap);
            Messagepz messagepz = this.messagepzMapper.selectOne(new LambdaQueryWrapper<Messagepz>().eq(Messagepz::getPath, url));
            if (messagepz!=null){
                Messageconfiguration messageconfiguration = this.messageconfigurationMapper.selectById(messagepz.getUid());
                if (messageconfiguration!=null){
                    if (StringUtils.isNotEmpty(messagepz.getPersonnel())){
                        String tc001 = reqParam.split("tc001=")[1].split(",")[0];
                        String tc002 = reqParam.split("tc002=")[1].split(",")[0];
                        String udf12 = reqParam.split("udf11=")[1].split(",")[0];
                        if (messagepz.getPersonnel().contains(",")){
                            String[] split = messagepz.getPersonnel().split(",");
                            for (String arr:split) {
                                Znx znx=Znx.builder().code(tc001+":"+tc002).status(0).content(udf12+messageconfiguration.getContent())
                                        .notificationCode(arr).processId(null).title("通知").creationTime(DateUtil.format(new Date(), "yyyy-MM-dd")).build();
                                this.znxMapper.insert(znx);
                            }
                        }else {
                            Znx znx=Znx.builder().code(tc001+":"+tc002).status(0).content(udf12+messageconfiguration.getContent())
                                    .notificationCode(messagepz.getPersonnel()).processId(null).title("通知").creationTime(DateUtil.format(new Date(), "yyyy-MM-dd")).build();
                            this.znxMapper.insert(znx);
                        }
                    }
                }
            }
        }catch (Exception e){
            throw new YiFeiException(500,"消息通知发生异常"+e.getMessage());
        }
        return null;
    }

    @Override
    public void PushMessageToZNX(String url, String jsonString) throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();

        Map<String, Object> objectMap = objectMapper.readValue(jsonString, Map.class);
        Messagepz messagepz = this.messagepzMapper.selectOne(new LambdaQueryWrapper<Messagepz>().eq(Messagepz::getPath, url));
        if (messagepz!=null) {
            Messageconfiguration messageconfiguration = this.messageconfigurationMapper.selectById(messagepz.getUid());
            String content = messageconfiguration.getContent();
            // 定义正则表达式以匹配 &{variable}
            Pattern pattern = Pattern.compile("&\\{(\\w+)}");
            Map<String, Object> insertJsonMap = new HashMap<>();
            Matcher matcher = pattern.matcher(content);
            while (matcher.find()) {
                // 提取匹配的变量名
                String variableName = matcher.group(1);
                System.out.println("提取的变量: " + variableName + "值: " + objectMap.get(variableName));
                insertJsonMap.put(variableName, objectMap.get(variableName));
            }
            if(StringUtils.isEmpty(messagepz.getPersonnel())) return;
            List<String> list = new ArrayList<>();
            if (messagepz.getPersonnel().contains(",")) {
                String[] split = messagepz.getPersonnel().split(",");
                for(int i = 0; i < split.length; i++) list.add(split[i]);
            }else{
                list.add(messagepz.getPersonnel());
            }
            List<Znx> znxList = new ArrayList<>();
            for (String arr:list) {
                Znx znx=Znx.builder().code("").status(0).content(messageconfiguration.getContent())
                        .notificationCode(arr).processId(null).title(ZnxStatusEnum.TONGZHI.getValue()).creationTime(DateUtil.format(new Date(), "yyyy-MM-dd"))
                        .customField(objectMapper.writeValueAsString(insertJsonMap)).build();
                this.znxMapper.insert(znx);
                znxList.add(znx);
            }
//            System.out.println("####");
//            this.znxService.saveBatch(znxList);
//            System.out.println("****");
            WebSocketMessage.sendMessage(list);
//            System.out.println("++++");
        }
    }
}




